/**
 * Copyright 2013-2022 the original author or authors from the Jeddict project (https://jeddict.github.io/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.jeddict.jpa.modeler.properties.cascade;

import io.github.jeddict.jpa.spec.CascadeType;
import io.github.jeddict.jpa.spec.EmptyType;
import org.netbeans.modeler.core.ModelerFile;
import org.netbeans.modeler.properties.embedded.GenericEmbeddedEditor;

/**
 *
 * @author Gaurav Gupta
 */
public class CascadeTypePanel extends GenericEmbeddedEditor<CascadeType> {

    private ModelerFile modelerFile;
    private CascadeType cascadeType;

    @Override
    public void init() {
        initComponents();

    }

    @Override
    public CascadeType getValue() {
        cascadeType = new CascadeType();
        if (all_CheckBox.isSelected()) {
            cascadeType.setCascadeAll(new EmptyType());
        } else if (detach_CheckBox.isSelected() || merge_CheckBox.isSelected()
                || persist_CheckBox.isSelected() || refresh_CheckBox.isSelected()
                || remove_CheckBox.isSelected()) {
            if (detach_CheckBox.isSelected()) {
                cascadeType.setCascadeDetach(new EmptyType());
            }
            if (merge_CheckBox.isSelected()) {
                cascadeType.setCascadeMerge(new EmptyType());
            }
            if (persist_CheckBox.isSelected()) {
                cascadeType.setCascadePersist(new EmptyType());
            }
            if (refresh_CheckBox.isSelected()) {
                cascadeType.setCascadeRefresh(new EmptyType());
            }
            if (remove_CheckBox.isSelected()) {
                cascadeType.setCascadeRemove(new EmptyType());
            }
        } else {
            cascadeType = null;
        }
        return cascadeType;
    }

    @Override
    public void setValue(CascadeType cascadeType) {
        this.cascadeType = cascadeType;
        if (cascadeType == null) {
            manageCascadeAllState(false);
        } else if (cascadeType.getCascadeAll() != null) {
            manageCascadeAllState(true);
        } else {
            if (cascadeType.getCascadeDetach() != null) {
                detach_CheckBox.setSelected(true);
            }
            if (cascadeType.getCascadeMerge() != null) {
                merge_CheckBox.setSelected(true);
            }
            if (cascadeType.getCascadePersist() != null) {
                persist_CheckBox.setSelected(true);
            }
            if (cascadeType.getCascadeRefresh() != null) {
                refresh_CheckBox.setSelected(true);
            }
            if (cascadeType.getCascadeRemove() != null) {
                remove_CheckBox.setSelected(true);
            }
        }
    }

    private void manageCascadeAllState(boolean state) {
        if (state) {
            detach_CheckBox.setEnabled(false);
            merge_CheckBox.setEnabled(false);
            persist_CheckBox.setEnabled(false);
            refresh_CheckBox.setEnabled(false);
            remove_CheckBox.setEnabled(false);
        } else {
            detach_CheckBox.setEnabled(true);
            merge_CheckBox.setEnabled(true);
            persist_CheckBox.setEnabled(true);
            refresh_CheckBox.setEnabled(true);
            remove_CheckBox.setEnabled(true);
        }
        all_CheckBox.setSelected(state);
        detach_CheckBox.setSelected(state);
        merge_CheckBox.setSelected(state);
        persist_CheckBox.setSelected(state);
        refresh_CheckBox.setSelected(state);
        remove_CheckBox.setSelected(state);

    }

    private void manageOtherState() {
        if (detach_CheckBox.isSelected() && merge_CheckBox.isSelected()
                && persist_CheckBox.isSelected() && refresh_CheckBox.isSelected()
                && remove_CheckBox.isSelected()) {
            manageCascadeAllState(true);
        }
//        else {
//            manageCascadeAllState(false);
//        }

    }

    public CascadeTypePanel(ModelerFile modelerFile) {
        this.modelerFile = modelerFile;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        main_LayeredPane = new javax.swing.JLayeredPane();
        all_CheckBox = new javax.swing.JCheckBox();
        persist_CheckBox = new javax.swing.JCheckBox();
        merge_CheckBox = new javax.swing.JCheckBox();
        remove_CheckBox = new javax.swing.JCheckBox();
        refresh_CheckBox = new javax.swing.JCheckBox();
        detach_CheckBox = new javax.swing.JCheckBox();

        main_LayeredPane.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Cascade", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N
        main_LayeredPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 15, 15));

        all_CheckBox.setSelected(true);
        all_CheckBox.setText("All");
        all_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                all_CheckBoxActionPerformed(evt);
            }
        });
        main_LayeredPane.add(all_CheckBox);

        persist_CheckBox.setText("Persist");
        persist_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                persist_CheckBoxActionPerformed(evt);
            }
        });
        main_LayeredPane.add(persist_CheckBox);

        merge_CheckBox.setText("Merge");
        merge_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                merge_CheckBoxActionPerformed(evt);
            }
        });
        main_LayeredPane.add(merge_CheckBox);

        remove_CheckBox.setText("Remove");
        remove_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remove_CheckBoxActionPerformed(evt);
            }
        });
        main_LayeredPane.add(remove_CheckBox);

        refresh_CheckBox.setText("Refresh");
        refresh_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refresh_CheckBoxActionPerformed(evt);
            }
        });
        main_LayeredPane.add(refresh_CheckBox);

        detach_CheckBox.setText("Detach");
        detach_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detach_CheckBoxActionPerformed(evt);
            }
        });
        main_LayeredPane.add(detach_CheckBox);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(main_LayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(main_LayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void all_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_all_CheckBoxActionPerformed
        manageCascadeAllState(all_CheckBox.isSelected());
    }//GEN-LAST:event_all_CheckBoxActionPerformed

    private void persist_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_persist_CheckBoxActionPerformed
        manageOtherState();
    }//GEN-LAST:event_persist_CheckBoxActionPerformed

    private void merge_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_merge_CheckBoxActionPerformed
        manageOtherState();
    }//GEN-LAST:event_merge_CheckBoxActionPerformed

    private void remove_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remove_CheckBoxActionPerformed
        manageOtherState();
    }//GEN-LAST:event_remove_CheckBoxActionPerformed

    private void refresh_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refresh_CheckBoxActionPerformed
        manageOtherState();
    }//GEN-LAST:event_refresh_CheckBoxActionPerformed

    private void detach_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detach_CheckBoxActionPerformed
        manageOtherState();
    }//GEN-LAST:event_detach_CheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox all_CheckBox;
    private javax.swing.JCheckBox detach_CheckBox;
    private javax.swing.JLayeredPane main_LayeredPane;
    private javax.swing.JCheckBox merge_CheckBox;
    private javax.swing.JCheckBox persist_CheckBox;
    private javax.swing.JCheckBox refresh_CheckBox;
    private javax.swing.JCheckBox remove_CheckBox;
    // End of variables declaration//GEN-END:variables
}
